package com.niodata.dp.core.resource;

import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface ResourceMapper {

  @Insert(
        "insert into RESOURCE (RES_ID,RES_PATH,OWNER_TYPE,OWNER_ID,RES_TYPE"
              + ") values (#{res.id},#{res.path},"
              + "#{res.ownerType},#{res.ownerId},#{res.typeName})")
  @Options(useGeneratedKeys = true, keyProperty = "res.id", keyColumn = "ID")
  void saveResource(@Param("res") ResourceDetail resourceDetail);

  @Select("select * from RESOURCE where RES_ID=#{id}")
  @ResultMap("resourceDetailMap")
  ResourceDetail getResourceById(@Param("id") String id);

  @Update("update RESOURCE set RES_PATH=#{res.path},OWNER_TYPE=#{res.ownerType},"
        + "OWNER_ID=#{res.ownerId},"
        + "TYPE=#{res.typeName} where RES_ID=#{res.id}")
  void updateResource(@Param("res") ResourceDetail resourceDetail);

  @Delete("delete from RESOURCE where RES_ID=#{id}")
  void deleteResource(@Param("id") String id);

  @Delete("delete from RESOURCE where RES_PATH=#{path} and RES_TYPE=#{type}")
  void deleteResourceByPath(@Param("path") String path, @Param("type") String type);

  @Select("select * from RESOURCE where OWNER_ID=#{ownerId} and OWNER_TYPE=#{ownerType} "
        + "and RES_TYPE=#{type}"
        + "limit #{maxCount} offset #{offset}")
  @ResultMap("resourceDetailMap")
  List<ResourceDetail> getResourceList(@Param("ownerId") long ownerId,
        @Param("ownerType") String ownerType,
        @Param("type") String type,
        @Param("offset") int offset,
        @Param("maxCount") int maxCount);


  @Select("select * from RESOURCE where RES_TYPE=#{type} and RES_PATH=#{path}")
  @ResultMap("resourceDetailMap")
  ResourceDetail getResourceByPath(@Param("path") String path, @Param("type") String type);

  List<ResourceDetail> getResourceByPathPrefix(@Param("path") String path,
        @Param("type") String type);

  List<ResourceDetail> getResourceByPrefix(@Param("prefix") String prefix,
        @Param("type") String type);


  @Select("select distinct a.* from (select r.* from RESOURCE r where r.RES_TYPE=#{resType} and "
        + " r.OWNER_TYPE='USER'and r.OWNER_ID=#{userId} "
        + " union all "
        + " select r.* from RESOURCE r,USER_GROUP ug where r.RES_TYPE=#{resType} "
        + " and r.OWNER_TYPE='GROUP' and r.OWNER_ID=ug.GROUP_ID "
        + " and ug.USER_ID=#{userId} and ug.ROLE in ('MASTER','OWNER')) as a")
  @ResultMap("resourceDetailMap")
  List<ResourceDetail> getUserManagedResources(@Param("userId") long userId,
        @Param("resType") String type);

  @Select("select distinct a.* from (select r.* from RESOURCE r where r.RES_TYPE=#{resType} and "
        + " r.OWNER_TYPE='USER'and r.OWNER_ID=#{userId} "
        + " union all "
        + " select r.* from RESOURCE r,USER_GROUP ug where r.RES_TYPE=#{resType} "
        + " and r.OWNER_TYPE='GROUP' and r.OWNER_ID=ug.GROUP_ID "
        + " and ug.USER_ID=#{userId} and ug.ROLE in ('MASTER','OWNER') "
        + "union all "
        + "select distinct r.* from RESOURCE r ,RESOURCE_AUTH au where r.RES_PATH = au.RES_PATH "
        + "and au.TARGET_ID=#{userId} and au.TARGET_TYPE='USER' "
        + "union all "
        + "select distinct r.* from RESOURCE r ,RESOURCE_AUTH au,USER_GROUP ug "
        + " where r.RES_PATH = au.RES_PATH and ug.USER_ID=#{userId} and ug.GROUP_ID=au.TARGET_ID "
        + "and au.TARGET_TYPE='GROUP') as a")
  @ResultMap("resourceDetailMap")
  List<ResourceDetail> getUserViewableResources(@Param("userId") long userId,
        @Param("resType") String type);

}
